package com.match.competition_apply.utils;

import cn.afterturn.easypoi.word.WordExportUtil;
import com.match.competition_apply.entity.CompetitionSignUp;
import com.match.competition_apply.entity.vo.FileVo;
import org.apache.poi.xwpf.usermodel.XWPFDocument;


import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.Map;


public class SignUpWordUtil {
    public static void toWord(String fileName, HttpServletResponse response, Map<String ,Object> replaceMap) {
        response.reset();//清空缓存区，防止存在某些字符使得下载的文件格式错误
        response.setHeader("Access-Control-Expose-Headers","Content-Disposition");
        response.setCharacterEncoding("utf-8");
        //response.setContentType("application/octet-stream;charset=utf-8");
        response.setContentType("application/msword;charset=utf-8");
        try {
            response.setHeader("Content-Disposition", "filename=" + java.net.URLEncoder.encode(fileName,"utf-8"));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        //文件模板路径
        //获取当前ClassPath的绝对URL路径，本测试用例使用临时文件保存路径，在生产环境时可配置生产环境保存路径
        //String path = Thread.currentThread().getContextClassLoader().getResource("").getPath();
        String wordModelFile = "F:\\DcProject_FX\\service\\service_competition\\src\\main\\resources\\SignUpWord.doc";
        try {
            //调用方法
            //wordModelFile=path+"SignUpTableWord.doc";
            XWPFDocument doc = WordExportUtil.exportWord07(wordModelFile, replaceMap);
            ServletOutputStream outputStream = response.getOutputStream();
            doc.write(outputStream);
            outputStream.flush();
            outputStream.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
